"""
This module provides functions for calculating and manipulation q values
"""


class QValue:

    @staticmethod
    def comput_qvalues(pep_list):
        """
        Input: list of sorted posterior error probabilities (PEPs)
        Output: list including the corresponding q values
        """
        n = len(pep_list)       
        if n == 0:
            return []
        qvalues = [ pep_list[0] ]
        sum_terms = pep_list[0]
        for i in range(1, n):
            fdr = qvalues[i-1] + pep_list[i]/(i+1) - sum_terms/(i*(i+1))
            qvalues.append(fdr)
            sum_terms += pep_list[i]
        # the same PEPs get the same q values (largest qvalue)
        for i in range(n-2, 0, -1):
            if pep_list[i] == pep_list[i+1]:
                qvalues[i] = qvalues[i+1]           
        return qvalues          
        


